package com.kim.client.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kim.client.interfaces.IMessageRecord;
import com.kim.client.mapper.MessageRecordMapper;
import com.kim.common.entity.domian.MessageRecord;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
* 一对一消息表(MessageToOne) 服务实现类
* @author xhh
* @since 2024-12-12
*/
@ApplicationScoped
@Slf4j
public class MessageRecordService extends ServiceImpl<MessageRecordMapper, MessageRecord> implements IMessageRecord {


    @Inject
    private MessageRecordMapper dataMapper;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public MessageRecord saveMessageToOne (MessageRecord request) {
        save(request);
        return request;
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void deleteMessageToOneById (Integer id) {
        removeById(id);

    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public MessageRecord updateMessageToOne (MessageRecord request) throws IllegalAccessException, NoSuchMethodException {

        updateById(request);
        return request;
    }

    @Override
    public  Page<MessageRecord> listMessageToOne (MessageRecord request) throws IllegalAccessException, NoSuchMethodException {
        LambdaQueryWrapper<MessageRecord> query = Wrappers.lambdaQuery();
        Page<MessageRecord> page = new Page<>(request.getPage(),request.getPageSize());
        Page<MessageRecord> res = page(page, query);
        return res;
    }

    @Override
    public MessageRecord getMessageToOneById (Integer id){
        Optional<MessageRecord> data = Optional.ofNullable(getById(id));
        return data.orElse(null);
    }

}

